//
//  MySetViewController.m
//  ZAKER
//
//  Created by dllo on 15/9/1.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MySetViewController.h"

@interface MySetViewController ()

@property (nonatomic, retain)DeformationButton *deformationClear;
@property (nonatomic, retain)DeformationButton *deformationBtn;

@end


@implementation MySetViewController

-(void)dealloc
{
    
    [_deformationBtn release];
    [_deformationClear release];
    
    [super dealloc];
    
}





- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor =  RGBACOLOR(240, 51, 51, 1);
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 + 37.5 * myWidth, 10 * myHeight, 75 * myWidth, 40 * myHeight)];
    label.text = @"我的";
    label.textAlignment = 1;
    self.navigationItem.titleView = label;
    self.navigationController.navigationBar.translucent = NO;
    [label release];
    
    [self createImage];
    [self createButton];
    
}

#pragma mark - 设置颜色

- (UIColor *)getColor:(NSString *)hexColor
{
    unsigned int red,green,blue;
    NSRange range;
    range.length = 2;
    
    range.location = 0;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];
    
    range.location = 2;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
    
    range.location = 4;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
    
    return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green / 255.0f) blue:(float)(blue / 255.0f) alpha:1.0f];
    
}


#pragma mark- 上面的照片

-(void)createImage
{
    
    UIImageView *myImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300* myHeight)];
    myImage.image = [UIImage imageNamed:@"my01.jpg"];
    [self.view addSubview:myImage];
    [myImage release];
    
}

#pragma mark - 收藏, 夜间, 清理三个button

-(void)createButton
{
    self.deformationBtn = [[[DeformationButton alloc]initWithFrame:CGRectMake(120 * myWidth, 330 * myHeight , 130 * myWidth, 36 * myHeight) withColor:[self getColor:@"e13536"]] autorelease];
    [self.view addSubview:_deformationBtn];
    
    
    [_deformationBtn.forDisplayButton setTitle:@"我的收藏"forState:UIControlStateNormal];
    [_deformationBtn.forDisplayButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [_deformationBtn.forDisplayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_deformationBtn.forDisplayButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 6, 0, 0)];
    [_deformationBtn addTarget:self action:@selector(btnCollection:) forControlEvents:UIControlEventTouchUpInside];
    
    
    DeformationButton *deformationBtnNight = [[DeformationButton alloc]initWithFrame:CGRectMake(120* myWidth, 390 * myHeight , 130 * myWidth, 36 * myHeight) withColor:[self getColor:@"e13536"]];
    [self.view addSubview:deformationBtnNight];
    [deformationBtnNight.forDisplayButton setTitle:@"夜间模式"forState:UIControlStateNormal];
    [deformationBtnNight.forDisplayButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [deformationBtnNight.forDisplayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [deformationBtnNight.forDisplayButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 6, 0, 0)];
    [deformationBtnNight addTarget:self action:@selector(btnNight:) forControlEvents:UIControlEventTouchUpInside];
    [deformationBtnNight release];
    
    
    self.deformationClear = [[[DeformationButton alloc]initWithFrame:CGRectMake(120 * myWidth, 450 * myHeight , 130 * myWidth, 36 * myHeight) withColor:[self getColor:@"e13536"]] autorelease];
    [self.view addSubview:_deformationClear];
    [_deformationClear.forDisplayButton setTitle:@"清除缓存"forState:UIControlStateNormal];
    [_deformationClear.forDisplayButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [_deformationClear.forDisplayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_deformationClear.forDisplayButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 6, 0, 0)];
    [_deformationClear addTarget:self action:@selector(btnClear) forControlEvents:UIControlEventTouchUpInside];
    
    
    RemoveCache *cache = [[RemoveCache alloc]init];
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    CGFloat f = [cache floatWithPath:cachePath];
    [_deformationClear.forDisplayButton setTitle:[NSString stringWithFormat: @"清除数据 %.2lf M", f] forState:UIControlStateNormal];
    [cache release];
    
}
// 视图即将出现 button效果停止

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [_deformationBtn stopLoading];
    
}

#pragma mark -  点击事件

#pragma mark ** 收藏的点击方法

-(void)btnCollection:(DeformationButton *)button
{
    
    CollectionViewController *coll = [[CollectionViewController alloc]init];
    [self.navigationController pushViewController:coll animated:YES];
    [coll release];
    
}

#pragma mark ** 夜间的点击方法

-(void)btnNight:(DeformationButton *)button
{
    
    UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
    
    button.selected = !button.selected;
    
    if (button.selected) {
        
        window.alpha = 0.4f;
        
    } else {
        
     window.alpha = 1.0f;
        
    }
  
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)1.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        
        [button stopLoading];
        
    });
}

#pragma mark - 清缓的实现方法
-(void)btnClear
{
    
    RemoveCache *cache = [[RemoveCache alloc]init];
    [cache removeCache];
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    CGFloat f = [cache floatWithPath:cachePath];
    
    [_deformationClear.forDisplayButton setTitle:[NSString stringWithFormat: @"清除数据 %.2lf M", f] forState:UIControlStateNormal];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [_deformationClear stopLoading];
        
    });
    
     [cache release];
    
}

- (void)didReceiveMemoryWarning
{
 
    [super didReceiveMemoryWarning];
    
}


@end
